<!--注册用户-->
<template>
  <div class="main_box">
    <div class="register_panel">
      <div class="tips">
        <span>温馨提示：</span>
        如您已是注册用户，可直接登录。
      </div>
      <el-form :model="registerForm" :rules="registerRule" ref="regForm">
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input
            v-model="registerForm.mobile"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item class="inline_item" prop="captcha">
          <el-input
            v-model="registerForm.captcha"
            style="width: 400px"
            placeholder="请输入图片验证码"
          ></el-input>
          <div
            class="code_btn"
            v-if="codeImg"
            @click="getImgCode"
            v-html="codeImg"
          ></div>
          <div class="get_code" v-else>图片验证码</div>
        </el-form-item>
        <el-form-item class="inline_item" prop="smsCode">
          <el-input
            v-model="registerForm.smsCode"
            style="width: 400px"
            placeholder="请输入短信验证码"
          ></el-input>
          <div
            class="get_code"
            @click="getSmsByRegister"
            :class="{ gray: smsCode }"
          >
            {{ waitMsg }}
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            placeholder="请输入6-16位密码"
            autocomplete="new-password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="surePwd">
          <el-input
            v-model="registerForm.surePwd"
            placeholder="请确认密码"
            autocomplete="new-password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="registerForm.selectCheck"
            >我已阅读并同意</el-checkbox
          >
          <p class="link" @click="getAccountAgreement()">《用户协议》</p>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="register_btn"
            @click="onSubmit('regForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Api from '@/api';
import utils from '@/utils';
import md5 from 'md5';

export default {
  layout: 'userLay',
  data () {
    const vMobile = (rule, value, callback) => {
      if (value && utils.verifyPhone(value)) {
        callback()
      } else {
        callback(new Error('手机号格式不正确'));
      }
    };
    const vCaptcha = (rule, value, callback) => {
      if (value && /^[a-zA-Z0-9]{4}$/.test(value)) {
        callback()
      } else {
        callback(new Error('请输入四位图片验证码'));
      }
    };
    const vSmsCode = (rule, value, callback) => {
      if (value && /^[0-9]{6}$/.test(value)) {
        callback()
      } else {
        callback(new Error('请输入六位短信验证码'));
      }
    };
    const vPassword = (rule, value, callback) => {
      if (value && utils.verifyPwd(value)) {
        callback()
      } else {
        callback(new Error('密码中至少包含大写字母、小写字母、数字和特殊字符中的3种，且至少6个字符'));
      }
    };
    const vSurePwd = (rule, value, callback) => {
      if (!this.registerForm.password) {
        callback(new Error('请输入密码'));
      } else if (value && value != this.registerForm.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback()
      }
    };

    return {
      registerForm: {
        username: "",
        mobile: "",
        captcha: "",
        smsCode: "",
        password: "",
        surePwd: "",
        selectCheck: false
      },
      codeImg: "", //图片验证码
      smsCode: true,
      waitMsg: "获取验证码",
      wait: 60,
      registerRule: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: vMobile, trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入图片验证码', trigger: 'blur' },
          { validator: vCaptcha, trigger: 'blur' }
        ],
        smsCode: [
          { required: true, message: '请输入短信验证码', trigger: 'blur' },
          { validator: vSmsCode, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入6-16位密码', trigger: 'blur' },
          { validator: vPassword, trigger: 'blur' }
        ],
        surePwd: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: vSurePwd, trigger: 'blur' }
        ],
      }
    }
  },
  mounted () {
    this.getImgCode();
  },
  methods: {
    //获取商城协议
    getAccountAgreement () {
      let mes = "霸王条款";
      mes = mes.replace(/(\r\n)+/g, "<br>");
      this.$alert(mes, "温馨提示", {
        customClass: "conHeight",
        confirmButtonText: "同意",
        dangerouslyUseHTMLString: true,
        lockScroll: true
      }).then(() => {

      });
    },
    //获取图片验证码
    getImgCode () {
      Api.getCaptcha(this.$axios).then(res => {
        if (res.status == 200) {
          this.codeImg = res.data
        }
      })
    },
    //获取短信验证码
    getSmsByRegister () {
      if (this.wait == 60 && !this.smsCode) {
        let mobile = this.registerForm.mobile || "";
        if (!mobile.trim()) {
          this.$message({
            message: "请输入手机号码",
            type: "warning"
          });
          return false;
        }
        if (!utils.verifyPhone(mobile)) {
          this.$message({
            message: "手机号码格式不正确",
            type: "warning"
          });
          return false;
        }
        Api.getSmsCode(this.$axios).then(res => {
          if (res.status == 200) {
            this.timer();
            this.$message({
              message: "短信验证码已发送",
              type: "success"
            });
          }
        })
      }
    },
    // 秒后重新发送
    timer () {
      if (this.wait == 0) {
        this.wait = 60;
        this.waitMsg = "获取验证码";
        this.smsCode = false;
      } else {
        this.waitMsg = this.wait + "秒后重试";
        this.wait--;
        setTimeout(() => {
          this.timer();
        }, 1000);
      }
    },
    onSubmit (formName) {
      if (!this.registerForm.selectCheck) {
        this.$message({
          message: '请勾选同意用户协议',
          type: 'warning'
        })
        return;
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { password, ...other } = this.registerForm;
          let params = {
            ...other,
            password: md5(password)
          };
          Api.onRegister(this.$axios, params).then(res => {
            if (res.status == 200) {
              this.$message({
                message: "注册成功，请登录",
                type: "success"
              });
              this.$router.push({ path: '/user/login' })
            }
          })
        }
      });
    }
  },
  watch: {
    registerForm: {
      handler: function (form) {
        if (
          utils.verifyPhone(form.mobile)
        ) {
          this.smsCode = false;
        } else {
          this.smsCode = true;
        }
      },
      deep: true
    },
  }
}
</script>
<style lang="scss" scoped>
.main_box {
  border-top: 1px solid #f0efef;
  background: #fff;
  .register_panel {
    width: 520px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 32px;
    padding-bottom: 74px;
    /deep/ .el-menu {
      margin-bottom: 30px;
      li {
        font-size: 20px;
        font-weight: 500;
        color: #424242;
      }
    }
  }
  .tips {
    font-size: 14px;
    color: #c1c1c1;
    padding-bottom: 12px;
    > span {
      color: #fb363f;
    }
  }
}
.conHeight /deep/ {
  .el-message-box__content {
    height: 500px;
    overflow: scroll;
  }
}
.register_btn {
  width: 100%;
}
.get_code {
  width: 100px;
  height: 40px;
  display: inline-block;
  vertical-align: bottom;
  cursor: pointer;
  margin-left: 12px;
  background: rgba(230, 229, 229, 1);
  border: 1px solid rgba(224, 223, 223, 1);
  color: #5b5b5b;
  font-size: 14px;
  line-height: 40px;
  text-align: center;
  &.gray {
    cursor: not-allowed;
  }
}
.code_btn {
  width: 100px;
  height: 40px;
  display: inline-block;
  vertical-align: bottom;
  cursor: pointer;
  margin-left: 12px;
  border: 1px solid #ddd;
  border-radius: 1px;
  overflow: hidden;
}
p.link {
  cursor: pointer;
  font-size: 14px;
  color: #2b8dff;
  display: inline-block;
}
</style>